package com.sbsj.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sbsj.mapper.LinkUserRoleMapper;
import com.sbsj.mapper.RoleMapper;
import com.sbsj.models.TAccount;
import com.sbsj.models.TLinkUserRole;
import com.sbsj.models.TRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoleService {

    private final RoleMapper roleMapper;

    private final LinkUserRoleMapper linkUserRoleMapper;

    public RoleService(RoleMapper roleMapper, LinkUserRoleMapper linkUserRoleMapper) {
        this.roleMapper = roleMapper;
        this.linkUserRoleMapper = linkUserRoleMapper;
    }

    public List<TRole> findRoleByUser(String userid)
    {
        LambdaQueryWrapper<TLinkUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TLinkUserRole::getUserId,userid);
        List<TLinkUserRole> linkUserRoleList = linkUserRoleMapper.selectList(wrapper);
        List<TRole> rolelist = new ArrayList<>();
        for (TLinkUserRole linkUserRole : linkUserRoleList)
        {
            TRole role = roleMapper.selectById(linkUserRole.getRoleId());
            rolelist.add(role);
        }
        return rolelist;
    }
}
